<?php
/**
 * Created by PhpStorm.
 * User: jian
 * Date: 18-8-22
 * Time: 下午9:59
 */

namespace app\api\controller;

use app\api\controller\Conmon;
use think\Db;
use think\Request;

class Article extends Conmon
{

    /********** 笔记列表 *********/
    public function index ()
    {
        $request = Request::instance();
        $data    = $request->param();
        if (!array_key_exists('id', $data)) {
            $page     = $data['page'];      // 页数
            $listrows = $data['listrows'];  // 每页显示条数
            $order    = 'asc';              // 排序 默认osc
            if (array_key_exists('order', $data)) {
                $order = $data['order'];
            }
            // 每页显示条数不能超过20条
            if ($listrows > 20) {
                $this->return_msg(400, 'listrows超出范围');
            }
            $data = Db::table('sy_article')
                ->order('article_id ' . $order)
                ->paginate($listrows, false, ['list_rows' => 20, 'page' => $page]);
            if ($data) {
                $this->return_msg(200, '成功', $data);
            } else {
                $this->return_msg(400, '获取失败');
            }
        } else {
            $id       = $data['id'];        // 栏目ID
            $page     = $data['page'];      // 页数
            $listrows = $data['listrows'];  // 每页显示条数
            $order    = 'asc';              // 排序 默认osc
            if (array_key_exists('order', $data)) {
                $order = $data['order'];
            }
            if ($listrows > 20) {
                $this->return_msg(400, 'listrows超出范围');
            }
            $data = Db::table('sy_article')
                ->where('article_cid', 'eq', $id)
                ->order('article_id ' . $order)
                ->paginate($listrows, false, ['list_rows' => 20, 'page' => $page]);
            if ($data) {
                $this->return_msg(200, '成功', $data);
            } else {
                $this->return_msg(400, '获取失败');
            }
        }
    }

    /********** 获取内容 *********/
    public function get ($id)
    {
        $article = Db::table('sy_article')
            ->where('article_id', 'eq', $id)
            ->find();
        if ($article) {
            $this->return_msg(200, '获取成功', $article);
        } else {
            $this->return_msg(400, '获取失败');
        }
    }

    /********** 发布文章 *********/
    public function add ()
    {
        $request = Request::instance();
        $title   = $request->param('title');
        $content = $request->param('content');
        $cate_id = $request->param('cid');
        if (empty($title)) {
            $this->return_msg(400, '标题不能为空');
        }
        if (empty($content)) {
            $this->return_msg(400, '内容不能为空');
        }
        if (empty($cate_id)) {
            $this->return_msg(400, 'cate_id不能为空');
        }
        if (Db::table('sy_article')->where('article_title', 'eq', $title)->find()) {
            $this->return_msg(400, '文章已存在');
        }
        $data    = [
            'article_title'   => $title,
            'article_content' => $content,
            'article_cid'     => $cate_id,
            'article_time'    => date("Y-m-d H:i:s", time())
        ];
        $article = Db::table('sy_article')->insert($data);
        if ($article) {
            $this->return_msg(200, '发布成功');
        } else {
            $this->return_msg(400, '发布失败');
        }
    }

    /********** 修改文章 *********/
    public function update ()
    {
        $request = Request::instance();
        $id      = $request->param('id');
        $cate_id = $request->param('cid');
        $title   = $request->param('title');
        $content = $request->param('content');
        $data    = [
            'article_title'   => $request->param('title'),
            'article_content' => $request->param('content'),
            'article_cid'     => $request->param('cid'),
        ];
        $article = Db::table('sy_article')
            ->where('article_id', 'eq', $id)
            ->update($data);
        if ($article) {
            $this->return_msg(200, '修改成功');
        } else {
            $this->return_msg(400, '修改失败');
        }
    }

    /********** 删除文章 *********/
    public function delete ($id)
    {
        $article = Db::table('sy_article')
            ->where('article_id', 'eq', $id)
            ->delete();
        if ($article) {
            $this->return_msg(200, '删除成功');
        } else {
            $this->return_msg(400, '删除失败');
        }
    }
}